package com.aspire.boc.mms.wmf.log;

import java.util.ArrayList;

import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.aspire.boc.mms.util.ResourceManager;

public final class OperLogger {
	// 普通文件日志
	private static int logLevel = ResourceManager.getInstance().getIntValue(
			"wmf.log.level");
	private static int logCacheSize = ResourceManager.getInstance()
			.getIntValue("wmf.log.cacheSize");

	private static ArrayList<OperLog> logList = new ArrayList();

	public static boolean isLogEnabled() {
		return logLevel != 0;
	}
	
	public static void dblog(String content) {
		dblog(null, content);
	}
	public static void dblog(HttpServletRequest request, String content) {
		dblog(request, content, "");
	}
	public static void dblog(HttpServletRequest request, String content,
			String operType) {
		dblog(request, content, operType, Long.valueOf(0L));
	}

	public static void dblog(HttpServletRequest request, String content,
			String operType, Long timeCost) {

		if (!isLogEnabled())
			return;
		OperLog ol = new OperLog();
		// ol.setContent(content);
		// ol.setOpertype(operType);
		// ol.setCost(timeCost);

		if (request != null) {
			String dateTimeFormat = "";
			// ol.setCreatedate(DateUtil.getCurrentDateTime(dateTimeFormat));
			//
			// ol.setCreateip(request.getRemoteHost());
			//
			// User user = request.getSession().getAttribute("logon_user");
			// ol.setCreateuser(user.getLogonid());
		}

		write(ol);
	}

	private static synchronized void write(OperLog ol) {
		logList.add(ol);
		//LogUtil.log.debug("logList.size=" + logList.size());
		if (logList.size() > logCacheSize)
			try {
				for (OperLog ol2 : logList) {
					// ol2.create();
				}
				logList.clear();

			//	LogUtil.log.debug("logList cleared!");
			} catch (Exception e) {
				e.printStackTrace();
			//	LogUtil.log.error(e.getMessage());
			}
	}

	
	//普通日志处理
	public enum LOG_LEVEL {
		DEBUG, INFO, WARN, ERROR
	}

	public static void log(Object ownerObject,
			String message, LOG_LEVEL level) {
		Logger logger = LoggerFactory.getLogger(ownerObject.getClass());
		if (LOG_LEVEL.ERROR == level) {
			logger.error(message);
		} else if (LOG_LEVEL.DEBUG == level) {
			logger.debug(message);
		} else if (LOG_LEVEL.INFO == level) {
			logger.info(message);
		} else if (LOG_LEVEL.WARN == level) {
			logger.warn(message);
		} else if (LOG_LEVEL.ERROR == level) {
			logger.warn(message);
		}
	}

	public static  void multLog(Object ownerObject,String message,LOG_LEVEL... type){
		for (int i = 0; i < type.length; i++) {
			log(ownerObject, message,type[i]);
		}
	}
}
